package com.hks.service.impl;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hks.common.utils.RedisCache;
import com.hks.common.utils.ResponseResult;
import com.hks.mapper.TestMapper;
import com.hks.pojo.User;
import com.hks.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.HashMap;
import java.util.concurrent.TimeUnit;

@Service
public class TestServiceImpl extends ServiceImpl<TestMapper, User> implements TestService {

    @Autowired
    private TestMapper testMapper;
    @Autowired
    private RedisCache redisCache;

    @Override
    public String print() {
        return "hello world！";
    }


    @Override
    public ResponseResult getCode(String timestamp){
        //验证码生成工具
        CircleCaptcha circleCaptcha = CaptchaUtil.createCircleCaptcha(200, 100, 4, 5);
        //获取验证码字符
        String code = circleCaptcha.getCode();
        //将验证码存入redis，设置过期时间为120秒
        redisCache.setCacheObject("code:"+timestamp, code, 120, TimeUnit.SECONDS);
        String imageBase64 = circleCaptcha.getImageBase64Data();
        HashMap<String, Object> map = new HashMap<>();
        map.put("code",code);
        map.put("image",imageBase64);
        return new ResponseResult(200,"获取验证码成功",map);
    }
}
